<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIBlog
 *
 * 使用codeigniter框架开发的一个博客。
 * 
 * @package     ciblog
 * @author      Eric
 * @copyright   Copyright (c) 2011 - 2012, Eric
 * @license     GNU General Public License 2.0
 * @link        http://www.hebaijun.com
 * @version     1.0
 */

 //----------------------------------------------------------

 /**
  * 评论控制器
  *
  * 控制评论的一些显示信息
  *
  * @package     ciblog
  * @subpackage  Controllers
  * @category    Controllers
  * @author      Eric <baijun.he@163.com>
  * @link        http://www.hebaijun.com
  */
class Comment extends MY_Controller {

	/**
	 * 默认方法
	 *
	 * @access   public
	 * @return   void
	 */
	public function index()
	{
		$comment_post_id = $this->input->post('comment_post_ID', TRUE);
		if( $comment_post_id === '' || !is_numeric($comment_post_id) )
		{
			go_back();
		}

		//取得文章信息
		$type = $this->input->post('comment_post_type', TRUE); //post or page
		if( 'post' == $type )
		{
			$posts = $this->post_m->load_posts('*',array('limit'=>1,'post_id'=>$comment_post_id));
		}
		else if( 'page' == $type )
		{
			$posts = $this->page_m->load_pages('*',array('limit'=>1,'page_id'=>$comment_post_id));
		}

		if(empty($posts))
		{
			echo '{"status":0, "message": "评论失败：找不到内容"}';
			exit();
		}

		if( 'close' == $posts[0]->post_comment_status )
		{
			echo '{"status":0, "message": "评论失败：这篇文章关闭了评论功能"}';
			exit();
		}

		/** 如果库中已经存在当前ip为spam的comment则直接拒绝 */
		$ip = $this->input->ip_address();
		$this->load->model('comment_m');
		$spams = $this->comment_m->load_comments('*', array('comment_author_IP'=>$ip, 'comment_approved' => 'spam'));
		if(count($spams) > 0)
		{
			show_404();
		}

		$comment_author       = $this->input->post('author', TRUE);
		$comment_author_email = $this->input->post('email', TRUE);
		$comment_author_url   = $this->input->post('url', TRUE);
		$comment_content      = $this->input->post('comment', TRUE);
		$comment_approved     = $this->option_m->get_option('comment_approved');

		$comment = array(
			'comment_post_id' => $comment_post_id,
			'comment_author' => $comment_author,
			'comment_author_email' => $comment_author_email,
			'comment_author_url' => $comment_author_url,
			'comment_author_IP' => $ip,
			'comment_date' => now(),
			'comment_content' => $comment_content,
			'comment_approved' => $comment_approved,
			'comment_parent' => 0, //盖楼用，暂时没有实现
			'comment_user_id' => 0 //登录用户评论，暂时没有实现
		);

		$comment_insert_id = $this->comment_m->add_comment($comment);

		if($comment_insert_id === FALSE)
		{
			echo '{"status":0, "message": "评论失败：未知错误！"}';
			exit();
		}
		else
		{
			if( $comment_approved === '1')
			{
				$num = $this->post_m->refresh_comment_count($comment_post_id);

				//取得当前评论在第几页
				$comment_per_post = $this->option_m->get_option('comment_per_post');
				$page = floor(($num-1)/$comment_per_post) + 1;
				$url = base_url().$type.'/'.$comment_post_id.'/comment_pages/'.$page.'/comment/'.$comment_insert_id.'#comment-'.$comment_insert_id;
				//redirect('post/'.$comment_post_id.'/comment_pages/'.$page.'#comment-' . $comment_insert_id);
				echo '{"status":2, "message": "评论成功！", "url": "'.$url.'"}';
				exit();
			}
			else
			{
				//审核
				echo '{"status":1, "message": "评论成功，请等待管理员审核！"}';
				exit();
			}
		}	
	}

}

/* End of file comment.php */
/* Location: ./application/controllers/comment.php */